<?php
/*
 * Copyright (c) 2022.  Liuhongbin All rights reserved.
 * @domain http://dkyx.cc
 * @author liuhongbin <514660094@qq.com>
 */

namespace app\index\controller\v1\work_wechat;

use app\common\controller\WorkWechatController;
use app\common\model\wework\WeworkKfMsg;
use hiduoke\services\workWechat\WorkWechatOpen;
use hiduoke\services\WorkWechatService;
use think\facade\Cache;
use think\facade\Log;
use think\App;

class Event extends WorkWechatController
{

    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 微信客服消息事件
     * @return void
     */
    public function servicer()
    {
        try {
            $work = new WorkWechatService();
            $app = $work->getKfApp();
            $message = $app->server->getMessage();
            $this->dealWithKefuMsg($message, get_plaid());
            event('wework_event_message', ['message' => $message, 'plaid' => get_plaid()]);
            $response = $app->server->serve();
            $response->send();
        } catch (\Exception $e) {
            Log::write(["event_servicer：" => $e->getMessage() . '，文件' . $e->getFile() . '，行：' . $e->getLine()]);
        }
    }

    /**
     * 客户联系事件
     * @return void
     */
    public function customer()
    {
        try {
            $work = new WorkWechatService();
            $app = $work->getCustomerApp();
            $message = $app->server->getMessage();
            event("wework_customer_message", [
                'message' => $message,
                'plaid'   => get_plaid()
            ]);
            event('wework_event_message', ['message' => $message, 'plaid' => get_plaid()]);
            $response = $app->server->serve();
            $response->send();
        } catch (\Exception $e) {
            Log::write(["event_customer：" => $e->getMessage() . '，文件' . $e->getFile() . '，行：' . $e->getLine()]);
        }
    }

    /**
     * 自建应用消息事件
     * @return void
     */
    public function apps()
    {
        try {
            $work = new WorkWechatService();
            $app = $work->getAppsApp();
            $message = $app->server->getMessage();
            event("wework_apps_message", [
                'message' => $message,
                'plaid'   => get_plaid()
            ]);
            $response = $app->server->serve();
            $response->send();
        } catch (\Exception $e) {
            Log::write(["event_apps：" => $e->getMessage() . '，文件' . $e->getFile() . '，行：' . $e->getLine()]);
        }
    }


}
